package com.example.denglu.pages.News_pager;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.denglu.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class NewsActivity extends AppCompatActivity {

    private ImageView newspagerimg;
    private TextView newspagertitle;
    private TextView content;
    private Button ckpl;
    private String id;
    private RecyclerView ckpldata;
    private Button finshfun;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        getnews();
        initView();
        clickbut();
        clickfinsh();
    }

    private void clickfinsh() {
        finshfun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void clickbut() {

        ckpl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        OkHttpClient client = new OkHttpClient().newBuilder()
                                .build();
                        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
                        Request request = new Request.Builder()
                                .url("http://124.93.196.45:10001/prod-api/press/comments/list?newsId=" + id)
                                .method("GET", null)
                                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                                .build();
                        try {
                            Response response = client.newCall(request).execute();
                            client.newCall(request).enqueue(new Callback() {
                                @Override
                                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(NewsActivity.this, "请求网络服务器失败", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }

                                @Override
                                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                                    String string = response.body().string();
                                    try {
                                        JSONObject stringjsonObject = new JSONObject(string);

                                        int code = stringjsonObject.getInt("code");

                                        if (code == 200) {
                                            JSONArray rowsArr = stringjsonObject.getJSONArray("rows");

                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    ckpldata.setLayoutManager(new LinearLayoutManager(NewsActivity.this));
                                                    ckpldata.setAdapter(new RecyclerView.Adapter() {

                                                        private int index = -1;

                                                        class myho extends RecyclerView.ViewHolder {
                                                            TextView textView, textViews;

                                                            public myho(@NonNull View itemView) {
                                                                super(itemView);
                                                                textView = itemView.findViewById(R.id.usesname);
                                                                textViews = itemView.findViewById(R.id.usesplnr);
                                                            }
                                                        }

                                                        @NonNull
                                                        @Override
                                                        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                                                            View view = getLayoutInflater().inflate(R.layout.newsdata_layout, null, false);
                                                            myho myho = new myho(view);
                                                            return myho;
                                                        }

                                                        @Override
                                                        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                                                            myho holder1 = (myho) holder;

                                                            try {
                                                                holder1.textView.setText("用户：" + ((JSONObject) rowsArr.get(position)).getString("userName"));
                                                                holder1.textViews.setText(((JSONObject) rowsArr.get(position)).getString("content"));

                                                            } catch (JSONException e) {
                                                                runOnUiThread(new Runnable() {
                                                                    @Override
                                                                    public void run() {
                                                                        Toast.makeText(NewsActivity.this, "转换失败", Toast.LENGTH_SHORT).show();
                                                                    }
                                                                });
                                                            }
                                                            if (index == position) {

                                                            }
                                                        }

                                                        @Override
                                                        public int getItemCount() {
                                                            return rowsArr.length() / 10;
                                                        }
                                                    });
                                                }
                                            });

                                        }

                                    } catch (JSONException e) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(NewsActivity.this, "转换失败", Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                    }


                                }
                            });
                        } catch (IOException e) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(NewsActivity.this, "请求网络服务器失败", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                }).start();
            }
        });
    }

    private void getnews() {
        Intent intent = getIntent();
        id = intent.getStringExtra("id");

        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();
                MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
                Request request = new Request.Builder()
                        .url("http://124.93.196.45:10001/prod-api/press/press/" + id)
                        .method("GET", null)
                        .addHeader("Content-Type", "application/x-www-form-urlencoded")
                        .build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(NewsActivity.this, "请求网络服务器失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        String string = response.body().string();
                        try {
                            JSONObject jsonObject = new JSONObject(string);

                            int code = jsonObject.getInt("code");
                            if (code == 200) {
                                JSONObject datajson = jsonObject.getJSONObject("data");
                                String title = datajson.getString("title");
                                String datacontent = datajson.getString("content");
                                String url = "http://124.93.196.45:10001" + datajson.getString("cover");
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Glide.with(NewsActivity.this).load(url).into(newspagerimg);
                                        newspagertitle.setText(title);
                                        content.setText(Html.fromHtml(datacontent));
                                    }
                                });

                            } else {
                            }
                        } catch (JSONException e) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(NewsActivity.this, "转换失败", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                });
                try {
                    Response response = client.newCall(request).execute();
                } catch (IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(NewsActivity.this, "请求网络服务器失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();
    }

    private void initView() {
        newspagerimg = findViewById(R.id.newspagerimg);
        newspagertitle = findViewById(R.id.newspagertitle);
        content = findViewById(R.id.content);
        ckpl = findViewById(R.id.ckpl);
        ckpldata = findViewById(R.id.ckpldata);
        finshfun = findViewById(R.id.finshfun);
    }
}